package br.ufrj.ppgi.jemf_basic_example.fragments;

import java.util.Random;

import android.app.Fragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.Toast;
import br.ufrj.ppgi.jemf.core.coordination.EnumLevel;
import br.ufrj.ppgi.jemf.mobile.FeatureHolder.EnumFeatureType;
import br.ufrj.ppgi.jemf.mobile.bean.HealthCareUnit;
import br.ufrj.ppgi.jemf.mobile.factory.EmergencyManagementFactory;
import br.ufrj.ppgi.jemf_basic_example.R;

public class HealthCareUnitFragment extends Fragment implements OnClickListener, OnItemClickListener, OnItemLongClickListener {

	// JEMF.
	EmergencyManagementFactory emf;
	// Loader.
    private LoaderCallbacks<Cursor> loaderCall = null;
    // Loader ID.
	private final int loaderID = 5;



	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Instantiating JEMF.
		emf = EmergencyManagementFactory.newInstance(this.getActivity().getApplicationContext());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_health_care_unit, container, false);

		// Button Add Health Care Unit.
		Button button = (Button) rootView.findViewById(R.id.buttonHEALTHCAREUNIT);
		button.setOnClickListener(this);

		// Health Care Unit List.
		ListView listView = (ListView) rootView.findViewById(R.id.listViewHEALTHCAREUNIT);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);

		// Instantiating a Cursor Adapter to set in List View.
		CursorAdapter cursorAdapter = emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getAdapter();
		listView.setAdapter(cursorAdapter);

		return rootView;
	}

	@Override
	public void onResume() {
		// Instantiating a Loader to read data from database.
		this.loaderCall = emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getLoader(null, null, null);
		// Loading content.
		// Prepare the loader. Either re-connect with an existing one, or start a new one.
		if (getLoaderManager().getLoader(loaderID) != null && !getLoaderManager().getLoader(this.loaderID).isReset()) {
			this.getActivity().getLoaderManager().restartLoader(this.loaderID, null, this.loaderCall);
		} else {
			this.getActivity().getLoaderManager().initLoader(this.loaderID, null, this.loaderCall);
		}
		super.onResume();
	}

	@Override
	public void onClick(View arg0) {
		// Creating Random Health Care Unit.
		Random r = new Random();
        HealthCareUnit healthCareUnit = new HealthCareUnit();
        healthCareUnit.setName("Health Care Unit " + String.valueOf(r.nextInt()));
        healthCareUnit.setDescription("Health Care Unit description " + String.valueOf(r.nextLong()));
        healthCareUnit.setLevel(EnumLevel.values()[r.nextInt(EnumLevel.values().length)]);
        healthCareUnit.setCapacity(r.nextInt(1000));
		// Saving Health Care Unit.
		emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getManager().save(healthCareUnit);
        Toast.makeText(this.getActivity().getApplicationContext(), healthCareUnit.toString(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	    Cursor cursor = (Cursor) arg0.getAdapter().getItem(arg2);
	    if (cursor != null) {
	        // Updating Health Care Unit.
	        Random r = new Random();
	        HealthCareUnit healthCareUnit = emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getManager().deserialize(cursor);
	        healthCareUnit.setName("Health Care Unit " + String.valueOf(r.nextInt()));
	        healthCareUnit.setDescription("Health Care Unit description " + String.valueOf(r.nextLong()));
	        healthCareUnit.setLevel(EnumLevel.values()[r.nextInt(EnumLevel.values().length)]);
	        healthCareUnit.setCapacity(r.nextInt(2000));
			// Saving Health Care Unit.
			emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getManager().save(healthCareUnit);
	        Toast.makeText(this.getActivity().getApplicationContext(), healthCareUnit.toString(), Toast.LENGTH_SHORT).show();
	    }
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	    Cursor cursor = (Cursor) arg0.getAdapter().getItem(arg2);
	    if (cursor != null) {
			// Deleting Health Care Unit.
	        HealthCareUnit healthCareUnit = emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getManager().deserialize(cursor);
			emf.getMainFactory(EnumFeatureType.HEALTH_CARE_UNIT).getManager().remove(healthCareUnit);
	    }
		return true;
	}

}
